
package simuri.coherence;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Set;
import java.util.TreeSet;
import org.apache.log4j.Logger;
import simuri.uridb.DbConnection;
import simuri.utils.Configuration;

/**
 *
 * @author Anwender
 */
public class DbEdgeCountCoherence implements CoherenceFunction{
      
 static Logger logger = Logger.getLogger("AEON");
 public int DIRECT_EDGE_FACTOR = 10;
    /** Computes the log of the number of edges between uri1 and uri2
     * 
     * @param uri1 First URI
     * @param uri2 Second URI
     * @return Score
     */
    
    @Override
 public double getDbCoherence(String uri1, String uri2) {
     
        if (uri1.equals(uri2)) {
            return -1;
        }         
   
   Connection con = DbConnection.getConnection();   
 
    double coh = 0.0;
     
    try{
      Statement stmt = con.createStatement();
      String sq ="SELECT weight FROM uricoherence WHERE uri1='" + uri1 + "' AND uri2='" + uri2 + "'";
        ResultSet rset = stmt.executeQuery(sq);
          while (rset.next()){
           coh = rset.getInt("weight");
           coh = coh *DIRECT_EDGE_FACTOR;
           coh = Math.log(coh + 1);
           logger.info("uri1:"+ uri1 + " uri2:"+ uri2 + "WEIGHT:"+ coh);
          }
 
        stmt.close();
        con.close();
        }catch (SQLException ex) {
            logger.info("SQL Fehler " + ex.getMessage());
        } 
      return coh;
    }
    

    /** Batch version of coherence computation
     * 
     * @param uris Set of URIs
     * @param endpoint Endpoint
     * @param graph Graph (null if not needed)
     * @return Coherence Matrix
     */
    
    @Override
    public CoherenceMatrix getDbCoherence(Set<String> uris) {
        CoherenceMatrix c = new SparseCoherenceMatrix();
        for (String uri1 : uris) {
            for (String uri2 : uris) {
                if (!uri1.equals(uri2)) {
                    c.setCoherence(uri1, uri2, getDbCoherence(uri1, uri2));
                    //logger.info("Coherence between "+uri1+" and "+uri2+" is "
                    //        +c.getCoherence(uri1, uri2));                            
                }
            }
        }
        c.normRows();
        for (String uri1 : uris) {
            c.setCoherence(uri1, uri1, 1.0);
        }
        return c;
    }

    public static void main(String args[]) {
        DbEdgeCountCoherence ecc = new DbEdgeCountCoherence();
        TreeSet<String> uris = new TreeSet<>();
        uris.add("http://dbpedia.org/resource/Jena,_Louisiana");
        uris.add("http://dbpedia.org/resource/Louisiana");
        uris.add("http://dbpedia.org/resource/Louisiana,_Missouri");
        System.out.println(ecc.getDbCoherence(uris));
    }

    
    @Override
    public CoherenceMatrix getDbCoherence(Set<String> uris, Configuration c) {
        SparseCoherenceMatrix coherence = (SparseCoherenceMatrix) getDbCoherence(uris);
        //add tree distance information
        return coherence;
    }
 
}
